local Wnd_LobbyClass = class(WindowBase)

function Wnd_LobbyClass:Start(WindowIndex)
    self.WindowIndex = WindowIndex
    Event.AddListener(EventType.UILobbyEnter, self.Show, self)
    Event.AddListener(EventType.UILobbyExit, self.Hide, self)
    Event.AddListener(EventType.UserInfo, self.UpdateUserInfo, self)
    Event.AddListener(EventType.NameChange, self.ChangeName, self)
end

function Wnd_LobbyClass:OnNewInstance()
    local PlayBtn = Utils.FindChild(self.Instance, "QuickPlayBtn")
    self:Listen(PlayBtn, EventTriggerType.PointerClick, self.OnPlayClick)

    self.PlayerName = Utils.FindChild(self.Instance, "Name/Name")
    self.ChangeNameBtn = Utils.FindChild(self.Instance, "Name")
    self:Listen(self.ChangeNameBtn, EventTriggerType.PointerClick, self.ShowChangeNameWidget)

    self.ChangeNameWidget = Utils.FindChild(self.Instance, "ChangeNameWidget")
    self.InputName = Utils.FindChild(self.ChangeNameWidget, "InputField")
    self.ChangeNameSureBtn = Utils.FindChild(self.ChangeNameWidget, "SureBtn")
    self:Listen(self.ChangeNameSureBtn, EventTriggerType.PointerClick, self.SendChangeName)

    self.PlayerGold = Utils.FindChild(self.Instance, "Gold/Gold")
end

function Wnd_LobbyClass:OnShowDone()
    NetworkSender:AskUserInfo()
end

function Wnd_LobbyClass:OnPlayClick(Object)
    NetworkSender:StartMatch()
end

function Wnd_LobbyClass:UpdateUserInfo(UserInfo)
    LuaUtils:SetTMLabel(self.PlayerName, UserInfo.name)
end

function Wnd_LobbyClass:ChangeName(Player)
    LuaUtils:SetTMLabel(self.PlayerName, Player.Name)
    self.ChangeNameWidget:SetActive(false)
end

function Wnd_LobbyClass:SendChangeName(Object)
    local name = LuaUtils:GetTMInput(self.InputName)
    if name ~= "" then
        NetworkSender:ChangeName(name)
    else
        print("未输入名称！！！")
    end
end

function Wnd_LobbyClass:ShowChangeNameWidget(Object)
    self.ChangeNameWidget:SetActive(true)
end

function Wnd_LobbyClass:OnLostInstance()
    Event.RemoveListener(EventType.UILobbyEnter, self.Show)
    Event.RemoveListener(EventType.UILobbyExit, self.Hide)
    Event.RemoveListener(EventType.NameChange, self.ChangeName)
end

return Wnd_LobbyClass.new